package com.psbc.data.trans.biz;

import com.nantian.eftp.app.EFTP_APP;
import com.psbc.data.trans.dto.EdbDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import static com.psbc.data.trans.common.Constants.EDB_CLIENT_SUCCESS_CODE;
import static com.psbc.data.trans.common.Constants.EDB_INVOKE_SUCCESS_CODE;

/**
 * @author zhenyu.wu
 * @date 2024/4/15 16:56
 */
@Service
@Slf4j
public class EdbBiz {

    @Value("${edb.servername}")
    private String serverName;

    @Value("${edb.username}")
    private String userName;

    @Value("${edb.password}")
    private String passWord;

    public boolean edbTrans(EdbDto edbDto) {

        EFTP_APP eftp_APP = new EFTP_APP();

        try {
            int openResult = eftp_APP.eftp_open(serverName, userName, passWord, edbDto.getRecvSysCode());

            if (openResult >= EDB_CLIENT_SUCCESS_CODE) {

                log.info("file ftp start, file path is {}", edbDto.getFilePath());

                int invokeResult = eftp_APP.eftp_put(openResult, edbDto.getFilePath());

                if (EDB_INVOKE_SUCCESS_CODE.equals(invokeResult)) {
                    log.info("file ftp success");
                    return true;
                } else {
                    log.info("file ftp fail");
                    return false;
                }
            } else {
                return false;
            }
        } finally {
            eftp_APP.eftp_close();
        }
    }
}
